/*
==========================================================
Knight Class CPP
DevX Team
==========================================================
*/

#include "Board.h"
#include "Knight.h"
#include "Structures.h"
#include <Windows.h>
#include <conio.h>
#include <sstream>

/*
==========================================================
Knight Constructor
Description: initilize knight piece properties
==========================================================
*/
dxKnight::dxKnight( player_t thePlayer, int xPos, int yPos )
{
	this->pieceType=KNIGHT;
	this->currentPlayer=thePlayer;
	if (thePlayer==WHITE) {
		color_t = 0;} 
	else if (thePlayer==BLACK) {
		color_t = 1;
	this->piecePosition.x = xPos;
	this->piecePosition.y = yPos;
	}
}

/*
==========================================================
Knight Destructor
==========================================================
*/
dxKnight::~dxKnight()
{
}

/*
==========================================================
Return Knight Value
Description: Return the knight value
==========================================================
*/
int dxKnight::GetPieceValue(){
 return Value;
};

/*
==========================================================
Return Knight Color
Description: Return the block color where the piece is
==========================================================
*/
int dxKnight::GetBlockColor(int x, int y) {
  int x_Color,y_Color; 
  unsigned short int color;
  int selectedRow, selectedColumn, currentRow, currentColumn;
  selectedRow = dxBoard::GetSelectedRow();
  selectedColumn = dxBoard::GetSelectedColumn();
  currentRow = dxBoard::GetCurrentRow();
  currentColumn = dxBoard::GetCurrentColumn();
  if ((x == selectedColumn) && (y == selectedRow)) {
	  color = 10;
  } else if ((x == currentColumn) && (y == currentRow)) {
	  color = 9;
  } else {
  color = 7;}

  x_Color = x%2;
  y_Color = y%2;
  if (y_Color != 0) {
	  if (x_Color != 0) {
	  return 0;
	  }//end if (inner)
	  else{
	       return 1;
	  }//end else (inner)
  }//end if (outer)
  else {
	  if (x_Color != 0) {
	  return 1;
	  }//end if (inner)
	  else{
	      return 0;
          }//end else (inner)
  }//end else (outer)
}

/*
==========================================================
Draw Knight
Description: Draw knight on the board
==========================================================
*/
void dxKnight::Draw(int x, int y)
{
	// TODO
	//cout<<"Knight\t";
     CONSOLE_SCREEN_BUFFER_INFO coninfo; //variable to hold console info
     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //identifier to system created resource
     GetConsoleScreenBufferInfo(hConsole,&coninfo); //retrieves console info using variable as referance parameter.
	
	 int curX,curY,blockColor;

					curY = coninfo.dwCursorPosition.Y;
			        curX = coninfo.dwCursorPosition.X;

	                int tempX,tempY = 0;
			  
	        switch (x) {
	                case 0: tempY=3;
		            break;
		            case 1: tempY=8;
		            break;
		            case 2: tempY=13;
		            break;
		            case 3: tempY=18;
		            break;
		            case 4: tempY=23;
		            break;
		            case 5: tempY=28;
		            break;
		            case 6: tempY=33;
		            break;
		            case 7: tempY=38;
		            break;
		     }

		     switch (y) {
	                case 0: tempX=5;
		            break;
		            case 1: tempX=15;
		            break;
		            case 2: tempX=25;
		            break;
		            case 3: tempX=35;
		            break;
		            case 4: tempX=45;
		            break;
		            case 5: tempX=55;
		            break;
		            case 6: tempX=65;
		            break;
		            case 7: tempX=75;
		            break;
	         }
	         blockColor = GetBlockColor(x,y);
	       	 stringstream Spacer, 
				          Top, 
				          Middle, 
				          Bottom;
			 
			 if (color_t == 0) {
		     if (blockColor == 0) {
		     Top    << char(201) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205)<< char(205) << char(187)<<"\n";
		     Middle << char(186) << char(219) << char(75)  << char(110) << char(105) << char(103) << char(104) << char(116)<< char(219) << char(186)<<"\n";
		     Bottom << char(200) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205)<< char(205) << char(188)<<"\n";
		     Spacer << char(186) << char(219) << char(219) << char(219) << char(219) << char(219) << char(219) << char(219)<< char(219) << char(186)<<"\n";
		     } else {
		     Top    << char(201) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205)<< char(205) << char(187)<<"\n";
		     Middle << char(186) << char(255) << char(75)  << char(110) << char(105) << char(103) << char(104) << char(116)<< char(255) << char(186)<<"\n";
		     Bottom << char(200) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205) << char(205)<< char(205) << char(188)<<"\n";
		     Spacer << char(186) << char(255) << char(255) << char(255) << char(255) << char(255) << char(255) << char(255)<< char(255) << char(186)<<"\n";
			 }} else if (color_t == 1) {
             if (blockColor == 0) {
		     Top    << char(218) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196)<< char(196) << char(191)<<"\n";
		     Middle << char(179) << char(219) << char(75)  << char(110) << char(105) << char(103) << char(104) << char(116)<< char(219) << char(179)<<"\n";
		     Bottom << char(192) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196)<< char(196) << char(217)<<"\n";
		     Spacer << char(179) << char(219) << char(219) << char(219) << char(219) << char(219) << char(219) << char(219)<< char(219) << char(179)<<"\n";
		     } else {
		     Top    << char(218) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196)<< char(196) << char(191)<<"\n";
		     Middle << char(179) << char(255) << char(75)  << char(110) << char(105) << char(103) << char(104) << char(116)<< char(255) << char(179)<<"\n";
		     Bottom << char(192) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196) << char(196)<< char(196) << char(217)<<"\n";
		     Spacer << char(179) << char(255) << char(255) << char(255) << char(255) << char(255) << char(255) << char(255)<< char(255) << char(179)<<"\n";
			 }}
             
			  
             coninfo.dwCursorPosition.Y = tempY;
             coninfo.dwCursorPosition.X = tempX;
             SetConsoleCursorPosition(hConsole,coninfo.dwCursorPosition);
             cout<<Top.str();

             coninfo.dwCursorPosition.Y += 1;
             SetConsoleCursorPosition(hConsole,coninfo.dwCursorPosition);
             cout<<Spacer.str();

             coninfo.dwCursorPosition.Y += 1;
             SetConsoleCursorPosition(hConsole,coninfo.dwCursorPosition);
             cout<<Middle.str();

             coninfo.dwCursorPosition.Y += 1;
             SetConsoleCursorPosition(hConsole,coninfo.dwCursorPosition);
             cout<<Spacer.str();

             coninfo.dwCursorPosition.Y += 1;
             SetConsoleCursorPosition(hConsole,coninfo.dwCursorPosition);
             cout<<Bottom.str();
			  
}